<?php
	include_once 'isLogin.php';
	$add_success = null;
	$table_name = "company_product";
	$get_id = $_GET['id'];
	if($_GET['action']=='submit' ){

           if($_GET['id']!=false){
			$old = $commonc->getInfo($table_name,$_GET['id'],'img_0');
			$img_0 = $old['img_0'];
			
		}
		
          if($_FILES['img_0']['name']!=''){
             
			//设置路径
			$folder = "upload/image/";
			$annexFolder = $folder."large/";
			$middleFolder = $folder."middle/";
			$smallFolder = $folder."small/";
			//删除旧图
			@unlink($annexFolder.$img_0);
			@unlink($middleFolder.$img_0);
			@unlink($smallFolder.$img_0);
			//上传新图
			$upimg = new uploadImg();
			$img_0 = $upimg->upLoad('img_0',$annexFolder);
			//中图
			$upimg->smallImg($annexFolder.$img_0,$middleFolder);
			//小图
			$upimg->smallImg($annexFolder.$img_0,$smallFolder);
		}
                    
                if($_GET['id']!=false){
			$old = $commonc->getInfo($table_name,$_GET['id'],'img_1');
			$img_1 = $old['img_1'];
			
		}
		if($_FILES['img_1']['name']!=''){
			//设置路径
			$folder = "upload/image/";
			$annexFolder = $folder."large/";
			$middleFolder = $folder."middle/";
			$smallFolder = $folder."small/";
			//删除旧图
			@unlink($annexFolder.$img_1);
			@unlink($middleFolder.$img_1);
			@unlink($smallFolder.$img_1);
			//上传新图
			$upimg = new uploadImg();
			$img_1 = $upimg->upLoad('img_1',$annexFolder);
			//中图
			$upimg->smallImg($annexFolder.$img_1,$middleFolder);
			//小图
			$upimg->smallImg($annexFolder.$img_1,$smallFolder);
		}
                 if($_GET['id']!=false){
			$old = $commonc->getInfo($table_name,$_GET['id'],'img_2');
			$img_2 = $old['img_2'];
			
		}
		if($_FILES['img_2']['name']!=''){
			//设置路径
			$folder = "upload/image/";
			$annexFolder = $folder."large/";
			$middleFolder = $folder."middle/";
			$smallFolder = $folder."small/";
			//删除旧图
			@unlink($annexFolder.$img_2);
			@unlink($middleFolder.$img_2);
			@unlink($smallFolder.$img_2);
			//上传新图
			$upimg = new uploadImg();
			$img_2 = $upimg->upLoad('img_2',$annexFolder);
			//中图
			$upimg->smallImg($annexFolder.$img_2,$middleFolder);
			//小图
			$upimg->smallImg($annexFolder.$img_2,$smallFolder);
		}
		
		//插入数据库
		$data = array('name'=>$_POST['name'],'detail_description'=>$_POST['detail'],'time_imit'=>$_POST['time_imit'],'price'=>$_POST['price'],'img_0'=>$img_0,'img_1'=>$img_1,'img_2'=>$img_2,'company_id'=>$_POST['company_id']);
		if($_GET['id']!=false){
			$commonc->update($table_name,$data,"id='".$_GET['id']."'");
			$href = $_basename.'?id='.$_GET['id'];
		}else{
			$commonc->insert($table_name,$data);
			$href=$_basename;
			$id = $commonc->insert_id();
			$data = array('sort'=>$id);
			$commonc->update($table_name, $data, "id='".$id."'");
                        $get_id = $id;
                       
		}
		echo $commonc->submitAlert('成功',$href);
	}
	
        
        //传递公司id
	if($_GET['company_id']!=false&&$_GET['company_id']!=''){
		  $company_id = $_GET['company_id'];
	}
	//获得数据
	if( $get_id!=false){
          
		$row=$commonc->getInfo($table_name,$get_id);
		$smarty->assign('row',$row);
	}
	
        
         $smarty->assign("company_id", $company_id);
	 $smarty->display($_filename.".html");;
	
?>